﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using GNSCore.Helpers;
using GNSCore.Model;
using GNSCore.Settings;
using GNSCore.Managers;
using GNSCore.Repositories;
using GNSCore.Model.Context;
using GNSCore.Repositories.IOC;

namespace GNSWebSite.Controls
{
    /// <summary>
    /// 
    /// </summary>
    public partial class RegisterSettings : System.Web.UI.UserControl
    {
        IRepository<GNSCore.Model.UserSettings> _settingsRepository;
        IGNSContext _context;
        private string justCrUserName;
        private string mail;
        public string EMailAdress
        {
            set
            {
                mail = value;
            }
        }

        public string justCreatedUserName
        {
            set
            {
                justCrUserName = value;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        private void InitDependencies()
        {
            _settingsRepository = IOCManager.IOCContainer.Resolve<IRepository<UserSettings>>();
            _context = IOCManager.IOCContainer.Resolve<IGNSContext>();
            _settingsRepository.SetContext(_context);
        }

        protected void cvUsePhone_ServerValidate(object source, ServerValidateEventArgs args)
        {
            args.IsValid = chkUsePhone.Checked ? !string.IsNullOrEmpty(txtPhone.Text) : true;
        }

        public void ShowUserSettings()
        {
            InitDependencies();
            SettingsManager manager = new SettingsManager(_settingsRepository);
            UserSettings ctxu = manager.GetUserSettings(Page.User.Identity.Name);
            //todo: give adequate value to chkUseEMail
            chkUseEMail.Checked = ctxu.UseEmail;
            // lblEMail.Text += mail;
            chkUsePhone.Checked = ctxu.UseSMS;
            txtPhone.Text = ctxu.PhoneNumber;
            chkUseTemperature.Checked = ctxu.UseTemperature;
            txtTemperatureProcent.Text = ctxu.TemperatureProcent.ToString();
            txtTemperaturePeriod.Text = ctxu.TemperaturePeriod.ToString();
            chkUsePressure.Checked = ctxu.UsePressure;
            txtMaxPressure.Text = ctxu.MaxPressure.ToString();
            txtMinPressure.Text = ctxu.MinPressure.ToString();
            chkUseFogMaxProcent.Checked = ctxu.UseFogMaxProcent;
            txtFogMaxProcent.Text = ctxu.FogMaxProcent.ToString();
            chkUseHumidityMaxProcent.Checked = ctxu.UseHumidityMaxProcent;
            txtHumidityMaxProcent.Text = ctxu.HumidityMaxProcent.ToString();
        }

        public void SaveUserSettings()
        {
            SettingsManager manager = new SettingsManager(_settingsRepository);
            manager.SaveSettingsForUser(justCrUserName, chkUseEMail.Checked,
                                      chkUsePhone.Checked,
                                      txtPhone.Text,
                                      chkUseTemperature.Checked,
                                      Convert.ToDouble(txtTemperatureProcent.Text),
                                      Convert.ToInt32(txtTemperaturePeriod.Text),
                                      chkUsePressure.Checked,
                                      Convert.ToDouble(txtMaxPressure.Text),
                                      Convert.ToDouble(txtMinPressure.Text),
                                      chkUseFogMaxProcent.Checked,
                                      Convert.ToDouble(txtFogMaxProcent.Text),
                                      chkUseHumidityMaxProcent.Checked,
                                      Convert.ToDouble(txtHumidityMaxProcent.Text));
        }
        public void ModifyUserSettings()
        {
            InitDependencies();
            SettingsManager manager = new SettingsManager(_settingsRepository);
            manager.ModifySettingsForUser(justCrUserName, chkUseEMail.Checked,
                                      chkUsePhone.Checked,
                                      txtPhone.Text,
                                      chkUseTemperature.Checked,
                                      Convert.ToDouble(txtTemperatureProcent.Text),
                                      Convert.ToInt32(txtTemperaturePeriod.Text),
                                      chkUsePressure.Checked,
                                      Convert.ToDouble(txtMaxPressure.Text),
                                      Convert.ToDouble(txtMinPressure.Text),
                                      chkUseFogMaxProcent.Checked,
                                      Convert.ToDouble(txtFogMaxProcent.Text),
                                      chkUseHumidityMaxProcent.Checked,
                                      Convert.ToDouble(txtHumidityMaxProcent.Text));
        }

    }
}